<?
/*

    This file is part of Blue Violin.

    Blue Violin is free software: you can redistribute it and/or modify
    it under the terms of the GNU Affero Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Blue Violin is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Affero Public License for more details.

    You should have received a copy of the GNU Affero Public License
    along with Blue Violin.  If not, see <http://www.gnu.org/licenses/>.

    Blue Violin  Copyright (C) 2008  Blue Violin, Inc. & Josh Watts <josh.watts@blueviolin.org>
	For more information, visit http://www.blueviolin.org/

    This program comes with ABSOLUTELY NO WARRANTY.

*/
define("DEBUGLEVEL", 1);
require_once("Database.php");

class Utility
{

	function Utility()
	{
	}


	function HandleException($e)
	{
		if (DEBUGLEVEL >= 1)
		{
			echo "<pre>" . $e->getTraceAsString() . "</pre><br/>";
			echo "<pre>" . $e->getMessage() . "</pre>";
			exit;
		}		
		if (DEBUGLEVEL >= 2)
		{
			exit;
		}
	}

	function DateAdd($nDays, $dtDate)
	{
		$_tmTime = $this->MakeTime($dtDate, "00:00");
		$_tmTime += $nDays * (24 * 3600);
		return date("Y-m-d", $_tmTime);
// 		$dtDate= ($dtDate ? $dtDate : date("Y-m-d"));
// 		return date($strFormat,strtotime($nDays . " days", strtotime($dtDate)));
	}

	function MakeTime($strDate, $strTime)
	{
		$this->GetTimeParts($strTime, $dtTime, $dtHour, $dtMin, $dtAmPm);
		$this->GetDateParts($strDate, $dtDate, $dtMonth, $dtDay, $dtYear);
		if ("pm" == $dtAmPm)
		{
			if (12 == $dtHour)
			{
				$dtHour = 0;
			}
			$dtHour = 12 + $dtHour;
		}
		$tTime = mktime($dtHour, $dtMin, 0, $dtMonth, $dtDay, $dtYear);
		return $tTime;
	}

	function GetTimeParts($strDate, &$dtTime, &$dtHour, &$dtMin, &$dtAmPm)
	{
		if ("" == $strDate)
		{
			$dtTime = null;
			$dtHour = null;
			$dtMin = null;
			$dtAmPm = null;
		}
		else
		{
			$dtTime = strtotime($strDate);
			$dtHour = date("h", $dtTime);
			$dtMin = date("i", $dtTime);
			$dtAmPm = date("a", $dtTime);
		}
		return;
	}

	function GetDateParts($strDate, &$dtTime, &$dtMonth, &$dtDay, &$dtYear)
	{
		if ("" == $strDate ||
			"0000-00-00" == $strDate)
		{
			$dtTime = null;
			$dtMonth = null;
			$dtDay = null;
			$dtYear = null;
		}
		else
		{
			$dtTime = strtotime($strDate);
			$dtMonth = date("m", $dtTime);
			$dtDay = date("d", $dtTime);
			$dtYear = date("Y", $dtTime);
		}
		return;
	}

	function StripPhoneNumber($strPhoneNumber)
	{
		$tPhone = str_replace("(", "", $strPhoneNumber);
		$tPhone = str_replace(")", "", $tPhone);
		$tPhone = str_replace("-", "", $tPhone);
		$tPhone = str_replace(" ", "", $tPhone);
		return $tPhone;
	}

	function FormatPhone($strPhone)
	{
		if (empty($strPhone))
		{
			return $strPhone;
		}
		$t_strPhone = ereg_replace("-", "", $strPhone);
		$t_strPhone = ereg_replace("\(", "", $t_strPhone);
		$t_strPhone = ereg_replace("\)", "", $t_strPhone);
		if (7 == strlen($t_strPhone))
		{
			sscanf($t_strPhone, "%3s%4s", $nPrefix, $nExchange);
		}
		elseif (10 == strlen($t_strPhone))
		{
			sscanf($t_strPhone, "%3s%3s%4s", $nArea, $nPrefix, $nExchange);
		}
		elseif (10 < strlen($t_strPhone))
		{
			sscanf($t_strPhone, "%3s%3s%4s%s", $nArea, $nPrefix, $nExchange, $nExtension);
		}
		else
		{
			return "unknown phone format: " . $t_strPhone;
		}
		$strRetVal = "";
		$strRetVal .= (true == isset($nArea)) ? "(" . $nArea . ") " : "";
		$strRetVal .= $nPrefix . "-" . $nExchange;
		$strRetVal .= (true == isset($nExtension)) ? "x" . $nExtension : "";
		return $strRetVal;
	}

	function UUID()
	{
		$mUUID = time() . md5(getmypid().uniqid(rand()).$_SERVER['SERVER_NAME']);;
		return $mUUID;
	}

	function IsSelected($strOption, $strValue)
	{
		if ($strOption == $strValue)
		{
			echo("selected");
		}
	}

	function IsChecked($strValue)
	{
		if ("yes" == $strValue)
		{
			echo("checked");
		}
	}




}

?>